/**   
 * Copyright: Copyright (c) 2015 xlz
 * 
 * @ClassName: LoginInterceptor.java
 * @Description: 用户登录验证拦截器
 *
 * @version: v1.0.0
 * @author: xuyiping
 * @date: 2015-4-20 上午9:37:24 
 *
 * Modification History:
 * Date         Author          Version            Description
 *---------------------------------------------------------*
 * 2014-1-13     xuyiping           v1.0.0               修改原因
 */
package com.news.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginInterceptor implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		// TODO Auto-generated method stub

	}

	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		String requestURI = request.getRequestURI();
		if (requestURI.indexOf("/system/") > 0) {
			// 说明处在编辑的页面
			HttpSession session = request.getSession();
			String username = (String) session.getAttribute("name");
			
			if (username != null) {
				// 登陆成功的用户
				return true;
			} else {
				// 没有登陆，转向登陆界面
				request.getRequestDispatcher("/html/login.html").forward(
						request, response);
				return false;
			}
		} else {
			return true;
		}

		// false表示拦截，不向下执行；true表示放行
		// return true;
	}

}
